package com.vegetable.modules.controller.baseData;

import com.vegetable.common.utils.R;
import com.vegetable.modules.entity.baseData.CategoryDO;
import com.vegetable.modules.form.CategoryQuery;
import com.vegetable.modules.service.baseData.ICategoryService;
import com.vegetable.modules.vo.CategoryDTO;
import com.vegetable.modules.vo.category.CategoryTreeDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 分类信息控制器
 * </p>
 * @author Administrator
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private ICategoryService ICategoryService;

    /**
     * 新增分类
     * @param categoryDO 分类信息
     * @return 操作结果
     */
    @PostMapping("/add")
    public R addCategory(@Validated @RequestBody CategoryDO categoryDO) {
        ICategoryService.addCategory(categoryDO);
        return R.success("");
    }

    /**
     * 删除分类
     * @param id 分类ID
     * @return 操作结果
     */
    @DeleteMapping("/delete/{id}")
    public R deleteCategory(@PathVariable Long id) {
        ICategoryService.deleteCategory(id);
        return R.success("");
    }

    /**
     * 修改分类
     * @param categoryDO 分类信息
     * @return 操作结果
     */
    @PutMapping("/update")
    public R<String> updateCategory(@Validated @RequestBody CategoryDO categoryDO) {
            ICategoryService.updateCategory(categoryDO);
            return R.success("");
    }

    /**
     * 查询分类列表
     * @param query 查询条件
     * @return 分类列表
     */
    @GetMapping("/list")
    public R <List<CategoryDTO>> getCategoryList(@Validated CategoryQuery query) {
        List<CategoryDTO> categoryDTOList = ICategoryService.getCategoryList(query);
        return R.data(categoryDTOList);
    }

    /**
     * 查询单个分类详情
     * @param id 分类ID
     * @return 分类详情
     */
    @GetMapping("/detail/{id}")
    public R <CategoryDTO> getCategoryById(@PathVariable Long id) {
            CategoryDTO categoryDTO = ICategoryService.getCategoryById(id);
            return R.data(categoryDTO);
    }

    @GetMapping("/tree")
    public  R  getCategoryTree() {
        List<CategoryTreeDTO> res = ICategoryService.getCategoryTree();
        return R.data(res);
    }

    @PostMapping("/sort")
    public  R  updateCategorySortOrder(@RequestParam Long id, @RequestParam Integer sortOrder) {
        return ICategoryService.updateCategorySortOrder(id, sortOrder);
    }
}